/*!
 * \file ActionPolicyMgr.cpp
 * \project	WonderTrader
 *
 * \author Wesley
 * \date 2020/03/30
 * 
 * \brief 动作策略管理器实现文件
 * 
 * 实现动作策略管理器的核心功能：
 * 1. 从配置文件加载动作策略规则
 * 2. 解析和管理不同类型的交易动作规则
 * 3. 建立品种到规则组的映射关系
 * 4. 提供规则查询和获取服务
 */
#include "ActionPolicyMgr.h"

#include "../Share/StdUtils.hpp"
#include "../WTSTools/WTSLogger.h"

#include "../Includes/WTSVariant.hpp"
#include "../WTSUtils/WTSCfgLoader.h"

USING_NS_WTP;

ActionPolicyMgr::ActionPolicyMgr()
{
}

ActionPolicyMgr::~ActionPolicyMgr()
{
}

bool ActionPolicyMgr::init(const char* filename)
{
	// 从配置文件加载动作策略配置
	WTSVariant* cfg = WTSCfgLoader::load_from_file(filename, true);
	if (cfg == NULL)
		return false;

	// 遍历配置文件中的所有规则组
	auto keys = cfg->memberNames();
	for (auto it = keys.begin(); it != keys.end(); it++)
	{
		const char* gpName = (*it).c_str();
		WTSVariant*	vGpItem = cfg->get(gpName);
		ActionRuleGroup& gp = _rules[gpName];

		// 解析规则组中的动作规则列表
		WTSVariant* vOrds = vGpItem->get("order");
		if(vOrds != NULL && vOrds->isArray())
		{
			for (uint32_t i = 0; i < vOrds->size(); i++)
			{
				WTSVariant* vObj = vOrds->get(i);
				ActionRule aRule;
				
				// 解析动作类型
				const char* action = vObj->getCString("action");
				uint32_t uLimit = vObj->getUInt32("limit");
				uint32_t uLimitS = vObj->getUInt32("limit_s");
				uint32_t uLimitL = vObj->getUInt32("limit_l");
				
				// 根据动作字符串设置动作类型
				if (wt_stricmp(action, "open") == 0)
					aRule._atype = AT_Open;
				else if (wt_stricmp(action, "close") == 0)
					aRule._atype = AT_Close;
				else if (wt_stricmp(action, "closetoday") == 0)
					aRule._atype = AT_CloseToday;
				else if (wt_stricmp(action, "closeyestoday") == 0)
					aRule._atype = AT_CloseYestoday;
				else 
				{
					// 记录未识别的动作类型错误
					WTSLogger::error("Loading action policy failed: unrecognized type %s", action);
					continue;
				}

				// 设置规则的限制参数
				aRule._limit = uLimit;
				aRule._limit_s = uLimitS;
				aRule._limit_l = uLimitL;
				aRule._pure = vObj->getBoolean("pure");
				gp.emplace_back(aRule);
			}
		}

		// 解析规则组的品种过滤器
		WTSVariant* filters = vGpItem->get("filters");
		if(filters!=NULL && filters->isArray() && filters->size()>0)
		{
			// 为每个品种建立到规则组的映射
			for (uint32_t i = 0; i < filters->size(); i++)
			{
				const char* commid = filters->get(i)->asCString();
				_comm_rule_map[commid] = gpName;
			}
		}
	}

	cfg->release();
	return true;
}

const ActionRuleGroup& ActionPolicyMgr::getActionRules(const char* pid)
{
	std::string gpName = "default";

	// 查找品种对应的规则组名称
	{
		auto it = _comm_rule_map.find(pid);
		if (it != _comm_rule_map.end())
			gpName = it->second;
	}

	// 获取规则组，如果不存在则使用默认规则组
	{
		auto it = _rules.find(gpName);
		if (it == _rules.end())
		{
			it = _rules.find("default");
			// 记录规则组不存在的警告信息
			WTSLogger::error("Action policy group %s not exists, changed to default group", gpName.c_str());
		}

		assert(it != _rules.end());
		return it->second;
	}
}
